import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;


public class XMLCreator {
	ArrayList<Author> roles; 
	Document doc;
	
	public void createDocument(ArrayList<Author> users) {
		//get instance of factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		
		roles = users;
		try {
			//get instance of builder
			DocumentBuilder db = dbf.newDocumentBuilder();
			
			//create an instance of DOM
			doc = db.newDocument();
			
			createDocTree();
			printToFile();
		}catch (ParserConfigurationException pce) {
			//dump it
			System.out.println("Error while trying to instantiate Documnet Builder + pce");
			System.exit(1);
		}
	}
	
	private void createDocTree() {
		
		
		//create root element
		org.w3c.dom.Element rootEle = doc.createElement("Users");
		doc.appendChild(rootEle);
		
		for(int temp = 0; temp < roles.size(); temp++) {
			Author a = roles.get(temp);
			
			org.w3c.dom.Element userEle = createUserElement(a);
			(rootEle).appendChild( userEle);
		}
	
	}


	private org.w3c.dom.Element createUserElement(Author a) {
		
		org.w3c.dom.Element userEle = doc.createElement("Author");
		
		
		org.w3c.dom.Element nameEle = doc.createElement("Name");
		org.w3c.dom.Text nameText = doc.createTextNode(a.getName());
		(nameEle).appendChild(nameText);
		(userEle).appendChild(nameEle);
		
		org.w3c.dom.Element confEle = doc.createElement("Conference");
		org.w3c.dom.Text confText = doc.createTextNode(a.getConference());
		(confEle).appendChild(confText);
		(userEle).appendChild(confEle);
		
		org.w3c.dom.Element manuEle = doc.createElement("Manuscript");
		org.w3c.dom.Text manuText = doc.createTextNode(a.getManuscript());
		(manuEle).appendChild(manuText);
		(userEle).appendChild(manuEle);
		
		org.w3c.dom.Element uwEle = doc.createElement("Underwear");
		org.w3c.dom.Text uwText = doc.createTextNode(a.getUnderwear());
		(uwEle).appendChild(uwText);
		(userEle).appendChild(uwEle);
		
		org.w3c.dom.Element handEle = doc.createElement("Hand");
		org.w3c.dom.Text handText = doc.createTextNode(a.getHand());
		(handEle).appendChild(handText);
		(userEle).appendChild(handEle);
		
		
		
		return userEle;
	}
	
	private void printToFile() {
		
		try {
			
			TransformerFactory tf = TransformerFactory.newInstance();
			Transformer transformer = tf.newTransformer();
			
			
			DOMSource source = new DOMSource(doc);
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
			
			
			StreamResult result = new StreamResult(new File("C:\\Users\\Patrick Black\\Documents\\test.xml"));
			
			transformer.transform(source, result);
			
			
			}catch (TransformerException tfe) {
			tfe.printStackTrace();
		}
		
		
	}
}
